﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Dao;
using Core.Entity;
using System.Data;

namespace Core.Business
{
    public class UserBus
    {
        private UserDao dao;
        public UserBus()
        {
            dao = new UserDao();
        }

        public bool insert(User user) {
            return dao.insert(user);
        }

        public bool update(User user) {
            return dao.update(user);
        }

        public bool delete(int id) {
            return dao.delete(id);
        }

        public bool delete(String ids) {
            return dao.delete(ids);
        }

        public User get(int id) {
            DataTable dt = dao.get(id);
            User user = new User();

            if (dt.Rows.Count > 0)
            {
                user.Id = Convert.ToInt32(dt.Rows[0]["USR_ID"]);
                user.UserTypeId = Convert.ToInt32(dt.Rows[0]["UST_ID"]);
                user.Username = dt.Rows[0]["USR_USERNAME"].ToString();
                user.Password = dt.Rows[0]["USR_PASSWORD"].ToString();
                user.Email = dt.Rows[0]["USR_EMAIL"].ToString();
                user.IsActive = Convert.ToBoolean(dt.Rows[0]["USR_IS_ACTIVE"]);
                
            }
            return user;
        }

        public User login(string login, string password) {
            DataTable dt = dao.login(login, password);
            User user = null;

            if (dt.Rows.Count > 0)
            {
                user = new User();
                user.Id = Convert.ToInt32(dt.Rows[0]["USR_ID"]);
                user.UserTypeId = Convert.ToInt32(dt.Rows[0]["UST_ID"]);
                user.Username = dt.Rows[0]["USR_USERNAME"].ToString();
                user.Password = dt.Rows[0]["USR_PASSWORD"].ToString();
                user.Email = dt.Rows[0]["USR_EMAIL"].ToString();
                user.IsActive = Convert.ToBoolean(dt.Rows[0]["USR_IS_ACTIVE"]);

            }
            return user;
        }

        public List<User> getAll() {
            DataTable dt = dao.getAll();
            List<User> list = new List<User>();

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    User user = new User();
                    user.Id = Convert.ToInt32(dr["USR_ID"]);
                    user.UserTypeId = Convert.ToInt32(dr["UST_ID"]);
                    user.Username = dr["USR_USERNAME"].ToString();
                    user.Password = dr["USR_PASSWORD"].ToString();
                    user.Email = dr["USR_EMAIL"].ToString();
                    user.IsActive = Convert.ToBoolean(dr["USR_IS_ACTIVE"]);

                    list.Add(user);
                }

            }
            return list;
        }

        public int getInsertedId()
        {
            int id = 0;
            try
            {
                id = Convert.ToInt32(dao.getInsertedId());
            }
            catch(Exception ex) {}

            return id;
        }

        public bool isUsernameExist(string username)
        {
            DataTable dt = dao.getByUsername(username);
            return dt.Rows.Count > 0;
        }

        public bool isEmailExist(string email)
        {
            DataTable dt = dao.getByEmail(email);
            return dt.Rows.Count > 0;
        }
    }
}
